package com.ruicar.afs.cloud.afscase.infomanagement.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.approvetask.service.WorkTaskPoolService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.mapper.CaseContractInfoMapper;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import com.ruicar.afs.cloud.seats.entity.UserCollocation;
import com.ruicar.afs.cloud.seats.service.UserCollocationService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-05-13 15:20
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class CaseContractInfoServiceImpl extends ServiceImpl<CaseContractInfoMapper, CaseContractInfo> implements CaseContractInfoService {

    private CaseContractInfoMapper caseContractInfoMapper;

    private CaseApproveRecordService caseApproveRecordService;

    private WorkTaskPoolService workTaskPoolService;

    private UserCollocationService userCollocationService;

    @Override
    public void updateApplyStatus(String contractNo, String applyStatus) {
        this.update(Wrappers.<CaseContractInfo>lambdaUpdate()
                .eq(StringUtils.isNotEmpty(contractNo), CaseContractInfo::getContractNo, contractNo)
                .set(CaseContractInfo::getApplyStatus, applyStatus));
    }

    @Override
    public List<CaseContractInfo> getAllContractInfoByWorkflowStatus(){
        List<String>  status =new ArrayList<>();
        status.add(AfsEnumUtil.key(GpsStatusEnum.SEND_TASK));
        status.add(AfsEnumUtil.key(GpsStatusEnum.WAIT_INSTALL));
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        Long time = calendar.getTimeInMillis();
        // 获取三天前的毫秒数
        Long before=time-(86400000*60);
        String thisTime = this.getYearMonthDayHourMinuteSecond(time);
        String beForeTime =this.getYearMonthDayHourMinuteSecond(before);
        return this.list(Wrappers.<CaseContractInfo>lambdaQuery()
                .in(CaseContractInfo::getGpsWorkflowStatus,status).between(beForeTime != null && thisTime != null, CaseContractInfo::getCreateTime,beForeTime, thisTime));
    }
    /**
     * 将毫秒转换为年月日时分秒
     *
     * @author GaoHuanjie
     */
    public String getYearMonthDayHourMinuteSecond(long timeMillis) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(timeMillis);
        int year=calendar.get(Calendar.YEAR);

        int month=calendar.get(Calendar.MONTH) + 1;
        String mToMonth=null;
        if (String.valueOf(month).length()==1) {
            mToMonth="0"+month;
        } else {
            mToMonth=String.valueOf(month);
        }

        int day=calendar.get(Calendar.DAY_OF_MONTH);
        String dToDay=null;
        if (String.valueOf(day).length()==1) {
            dToDay="0"+day;
        } else {
            dToDay=String.valueOf(day);
        }

        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        String hToHour=null;
        if (String.valueOf(hour).length()==1) {
            hToHour="0"+hour;
        } else {
            hToHour=String.valueOf(hour);
        }

        int minute=calendar.get(Calendar.MINUTE);
        String mToMinute=null;
        if (String.valueOf(minute).length()==1) {
            mToMinute="0"+minute;
        } else {
            mToMinute=String.valueOf(minute);
        }

        int second=calendar.get(Calendar.SECOND);
        String sToSecond=null;
        if (String.valueOf(second).length()==1) {
            sToSecond="0"+second;
        } else {
            sToSecond=String.valueOf(second);
        }
        return  year+ "-" +mToMonth+ "-" +dToDay+ " "+hToHour+ ":" +mToMinute+ ":" +sToSecond;
    }


    @Override
    public void updateWorkflowStatus(String contractNo, GpsStatusEnum statusEnum) {
        this.update(Wrappers.<CaseContractInfo>lambdaUpdate()
                .eq(StringUtils.isNotBlank(contractNo), CaseContractInfo::getContractNo, contractNo)
                .set(CaseContractInfo::getGpsWorkflowStatus, AfsEnumUtil.key(statusEnum)));
    }

    @Override
    public void updateLoanAuditor(String contractNo, String userName) {
        this.update(Wrappers.<CaseContractInfo>lambdaUpdate()
                .eq(StringUtils.isNotEmpty(contractNo), CaseContractInfo::getContractNo, contractNo)
                .set(CaseContractInfo::getLoanAuditor, userName));
    }

    @Override
    public List<CaseContractInfo> getOverdueContractByChannel(String dealerNo){
        return this.caseContractInfoMapper.getOverdueContractByChannel(dealerNo);
    }

    @Override
    public void saveReviewSign(String contractNo, Boolean status){
        String reviewSign="";
        if(status){
            reviewSign="yes";
        }else{
            reviewSign="no";
        }
        this.update(Wrappers.<CaseContractInfo>lambdaUpdate()
                .eq(StringUtils.isNotEmpty(contractNo), CaseContractInfo::getContractNo, contractNo)
                .set(CaseContractInfo::getReviewSign, reviewSign));
    }

    @Override
    public void savePause(String contractNo, String dec){
        CaseContractInfo caseContractInfo=this.getOne(Wrappers.<CaseContractInfo>lambdaQuery().eq(CaseContractInfo::getContractNo,contractNo));
        String isLock=caseContractInfo.getIsLock();
        if(!isLock.equals(WhetherEnum.YES.getCode())){
            caseContractInfo.setRemarks(dec);
        }
        caseContractInfo.setApplyStatus(ApplyStatusEnum.SUSPEND_WAIT.getState());
        this.updateById(caseContractInfo);
    }

    /**
     * 判断是否有代理组长
     *
     * @return
     */
    @Override
    public Boolean getGroupLeader() {
        Boolean result = false;
        if(StringUtils.isNotBlank(SecurityUtils.getUsername())){
            UserCollocation userCollocation = userCollocationService.getOne(Wrappers.<UserCollocation>query().lambda()
                    .eq(UserCollocation::getLoginName,SecurityUtils.getUsername()));
            if(ObjectUtils.isNotEmpty(userCollocation) && StringUtils.isNotBlank(userCollocation.getAgentGroupLeaderLoginName())){
                result = true;
            }
        }
        return result;
    }

    /**根据经销商获取获取名下合同号码*/
    public List<String> getContractNoFromChannel(String channelNo){
        return this.baseMapper.getContractNoFromChannel(channelNo);
    }
}
